import { Body, Controller, Get, Param, Post, Query, Res } from '@nestjs/common';
// import { this.databaseService.executeQuery } from './tools/mysql';
import { DatabaseService } from './database.service';
import { Response } from 'express';
import { ToolsService } from 'src/utils/tools.service';
import {
  deleteFolderRecursive,
} from './file.operation';

@Controller('database')
export class DatabaseController {
  private tableName = '`test3`';

  constructor(private readonly databaseService: DatabaseService) {}

  @Post('create-table')
  async createTable(@Body() body: any) {
    let res = await this.databaseService.createTable(body);
    return res;
  }

  @Get('get-table-list')
  async getTableList() {
    let res = await this.databaseService.getTableList();
    return res;
  }

  @Get('get-table-structure')
  async getTableStructure(@Query('tableName') tableName: string) {
    if (!tableName) {
      ToolsService.fail('请传入需要查询的表名称');
    }
    let res = await this.databaseService.getTableStructure(tableName);
    return res;
  }

  @Post('remove-table')
  async removeTable(@Body() body: any) {
    if (!body.tableName) {
      ToolsService.fail('请传入需要删除的表名称');
    }
    let res = await this.databaseService.removeTable(body.tableName);
    return res;
  }

  @Post('add-table-field')
  async addTableField(@Body() body: any) {
    let res = await this.databaseService.addTableField(body);
    return res;
  }

  @Post('remove-table-field')
  async removeTableField(@Body() body: any) {
    let res = await this.databaseService.removeTableField(body);
    return res;
  }

  @Post('update-table-field')
  async updateTableField(@Body() body: any) {
    let res = await this.databaseService.updateTableField(body);
    return res;
  }

  // @Get('rename-table-field')
  // async renameTableField() {
  //   let field = 'field' + uuidv4().replace(/-/g, '');
  //   let sql = `ALTER TABLE ${
  //     this.tableName
  //   } RENAME COLUMN ${'name'} TO ${'name2'};`;
  //   let res = null;
  //   try {
  //     res = await this.databaseService.executeQuery(sql);
  //   } catch (error) {
  //     res = '更新失败';
  //   }
  //   return res;
  // }

  @Get('get-table-and-field-info')
  async getTableAndFieldInfo() {
    let res = await this.databaseService.getTableAndFieldInfo();
    return res;
  }

  // 更改字段的数据类型
  //   @Get('update-table-field')
  //   async updateTableField() {
  //     let field = 'field' + uuidv4().replace(/-/g, '');
  //     let sql = `ALTER TABLE ${
  //       this.tableName
  //     } MODIFY COLUMN ${'name'} ${'varchar(150)'};`;
  //     let res = null;

  //     try {
  //       res = await this.databaseService.executeQuery(sql);
  //     } catch (error) {}
  //     return res;
  //   }

  @Get('generate-and-stream')
  async generateAndStreamFile(
    @Query('tableName') tableName: string,
    @Res() res: Response,
  ) {
    let { tarStream } = await this.databaseService.generateAndStreamFile(
      tableName,
    );

    tarStream.pipe(res);

    tarStream.on('finish', () => {
      console.log('压缩完成');
    });
    tarStream.on('close', () => {
      deleteFolderRecursive(
        process.cwd() + '/docflow/await-download/' + tableName,
      );
    });

    // res.send(ejsdata);
  }
}
